/*
 * 系统参数窗口
 * 舞者无罪
 * 2014.11.24
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "sys.h"
#include "com.h"
#include "mhlabel.h"
#include "belt.h"
#include "keypane.h"
#include "mh/common.h"
#include "info.h"
#include "select.h"
#include "dumu_dist.h"
#include "sink_drt.h"
#include "rab_mode.h"
#include "dlg_ask.h"

#define NEEDLE_ZERO_CAPTION_WIDTH     160
#define NEEDLE_ZERO_CAPTION_HEIGHT    24
#define NEEDLE_ZERO_NAME_WIDTH        60
#define NEEDLE_ZERO_INTER             6
#define NEEDLE_ZERO_CAPTION_LEFT   (CX_EDGE_LEFT * 2)
#define NEEDLE_ZERO_CAPTION_TOP    (CY_TITLE4_HEIGHT + CX_EDGE_LEFT * 2)


/*
 *
 */
static const char* GetCamLockCaption(uchar lock)
{
    const char *sc = NULL;
    if(lock == 2)
    {
        sc = GetString(613);
    }
    else if(lock == 1)
    {
        sc = GetString(612);
    }
    else
    {
        sc = GetString(339);
    }
    return sc;
}
/*
 *
 */
void FlushSysWndSinkEnable(HWND hWnd)
{
    HWND hTmp = GetDlgItem(hWnd, IDC_EDIT_SINK_ENABLE);
    uchar n = GetSinkEnable(g_knit_data);
    SetWindowCaption(hTmp, n ? MW_OPEN_CAPTION : MW_CLOSE_CAPTION);
    InvalidateRect(hTmp, NULL, TRUE);
}
/*
 *
 */
void FlushSysWndBelt(HWND hWnd)
{
    char sc[128] = {0};
    HWND hTmp = GetDlgItem(hWnd, IDC_EDIT_NEEDLE_ZERO);
    if(hTmp > 0)
    {
        sprintf(sc, "%d", GetNeedleZero(g_knit_data));
        SetWindowCaption(hTmp, sc);
        InvalidateRect(hTmp, NULL, TRUE);
    }
    hTmp = GetDlgItem(hWnd, IDC_EDIT_BELT_INTER);
    if(hTmp > 0)
    {
        sprintf(sc, "%d", GetBeltPitchAdj(g_knit_data));
        SetWindowCaption(hTmp, sc);
        InvalidateRect(hTmp, NULL, TRUE);
    }
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    data.pwnd_sys->change.sys = TRUE;
}
/*
 *
 */
void FlushSysWndSelectStart(HWND hWnd)
{
    HWND hTmp = GetDlgItem(hWnd, IDC_SELECT_START_SECT);
    char sc[64] = {0};
    sprintf(sc, "%u", GetSelectStartSect(g_knit_data));
    SetWindowCaption(hTmp, sc);
    InvalidateRect(hTmp, NULL, TRUE);
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    data.pwnd_sys->change.mac = TRUE;
}
/*
 *
 */
void FlushSysWndDumuMaxValue(HWND hWnd, int sys)
{
    HWND hTmp = GetDlgItem(hWnd, IDC_DUMU);
    char sc[64] = {0};
    sprintf(sc, "%d", GetDumuMaxValue(g_knit_data));
    SetWindowCaption(hTmp, sc);
    InvalidateRect(hTmp, NULL, TRUE);

    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    if(sys)
    {
        data.pwnd_sys->change.sys = TRUE;
    }
    else
    {
        data.pwnd_sys->change.other = TRUE;
    }
}
/*
 *
 */
void FlushSysWndRabType(HWND hWnd)
{
    char sc[32] = {0};
    uint mode = GetRabEnable(g_knit_data) ? 1 : 0;
    mode |= GetRollEnable(g_knit_data) ? 2 : 0;
    sprintf(sc, "%u", mode);
    HWND hTmp = GetDlgItem(hWnd, IDC_EDIT_HEAD_MOVE_ENABLE);
    SetWindowCaption(hTmp, sc);
    InvalidateRect(hTmp, NULL, TRUE);
}
/*
 *
 */
 static int GetGouZhenCom(HWND hWnd, int data)
 {
     int n = data - GetNeedleZero(g_knit_data);
     int total = GetBeltPitchAdj(g_knit_data);
     int ret = 0;
     if(total)
     {
         ret = n * GetTotalNeedle(g_knit_data) / total;
     }
     return ret;
 }
 /*
 *
 */
static void CreateSysInterface(HWND hWnd)
{
    char sc[256] = {0};
    //copy btn
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_COPY,
        15,
        10,
        CX_COPY_BMP,
        CY_COPY_BMP,
        hWnd, (int)&g_bmp[BMP_COPY]);
    //sys btn
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_XTCS_WND_BMP - CX_ASK_BMP * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_XTCS_WND_BMP - CX_ASK_BMP * 3 - 8,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_TRUE]);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_XTCS_WND_BMP - CX_ASK_BMP * 4 - 8 * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_ASK]);
    //left
    CreateWindow (MH_LABEL_NAME,
        MW_NEEDLE_ZERO_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%d", GetNeedleZero(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_NEEDLE_ZERO,
        NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_LEFT_YARN_RGHT_ZERO_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 1,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%d", GetLeftYarnRightCom(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_LEFT_YARN_RGHT_ZERO,
        NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 1,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_LEFT_YARN_LEFT_ZERO_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 2,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%d", GetLeftYarnLeftCom(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_LEFT_YARN_LEFT_ZERO,
        NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 2,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_RGHT_YARN_RGHT_ZERO_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 3,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%d", GetRightYarnRightCom(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_RGHT_YARN_RGHT_ZERO,
        NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 3,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_RGHT_YARN_LEFT_ZERO_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 4,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%d", GetRightYarnLeftCom(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_RGHT_YARN_LEFT_ZERO,
        NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 4,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_INCH_TYPE_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 5,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%.0f", GetInchType(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_INCH_TYPE,
        NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 5,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_SELECT_RGHT_COM_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 6,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%d", GetSelectRightCom(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_SELECT_RGHT_COM,
        NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 6,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_SELECT_LEFT_COM_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 7,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%d", GetSelectLeftCom(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_SELECT_LEFT_COM,
        NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 7,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_SELECT_SECT_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 8,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%u", GetSelectSect(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_SELECT_SECT,
        NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 8,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_HIGH_CAM_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 9,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%u", GetCamHighVoltage(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_HIGH_CAM,
        NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) *9,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_HIGH_XZQ_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 10,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%u", GetSelectHighVoltage(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_HIGH_XZQ,
        NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 10,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);
    //mid
    CreateWindow (MH_LABEL_NAME,
        MW_LOW_XZQ_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%u", GetGauzeHighVoltage(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_LOW_XZQ,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_DUMU_MADA_RESET_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 1,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%u", GetDumuResetSpeed(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_DUMU_RESET,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 1,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_DUMU_MADA_WORK_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 2,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%u", GetDumuWorkSpeed(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_DUMU_WORK,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 2,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_BELT_INTER_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 3,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%d", GetBeltPitchAdj(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_BELT_INTER,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 3,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_TOTAL_NEEDLE_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 4,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%u", GetTotalNeedle(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_TOTAL_NEEDLE,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 4,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_ROCK_SPEED_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 5,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%u", GetRockSpeed(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_ROCK_SPEED,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 5,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        GetString(121),
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 6,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_EDIT_NAME,
        GetSinkEnable(g_knit_data) ? MW_OPEN_CAPTION : MW_CLOSE_CAPTION,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_SINK_ENABLE,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 6,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_LANGUAGE_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 7,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_EDIT_NAME,
        GetLanguage(g_knit_data) ? MW_ENGLISH_CAPTION : MW_CHINESE_CAPTION,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_LANGUAGE,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 7,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_XZQ_REPEAT_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 8,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_EDIT_NAME,
        GetSelectRepeatEnable(g_knit_data) ? MW_OPEN_CAPTION : MW_CLOSE_CAPTION,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_XZQ_REPEAT,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 8,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        GetString(180),
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 9,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    uint mode = GetRabEnable(g_knit_data) ? 1 : 0;
    mode |= GetRollEnable(g_knit_data) ? 2 : 0;
    sprintf(sc, "%u", mode);
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_HEAD_MOVE_ENABLE,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 9,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_ROCK_COM_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 10,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%d", GetRockGapCom(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_ROCK_COM,
        CX_SCREEN / 3 + NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 10,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);
    //right
    CreateWindow (MH_LABEL_NAME,
        MW_ROCK_TYPE_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN * 2 / 3 + NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    BYTE n = GetRockType(g_knit_data);
    if(n == ROCK_FRONT_BACK)
    {
        sprintf(sc, "%s", MW_FRONT_BACK_ROCK_CAPTION);
    }
    else if(n == ROCK_BACK)
    {
        sprintf(sc, "%s", MW_BACK_ROCK_CAPTION);
    }
    else
    {
        sprintf(sc, "%s", MW_FRONT_ROCK_CAPTION);
    }
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_ROCK_TYPE,
        CX_SCREEN * 2 / 3 + NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_RAB_PERCENT_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN * 2 / 3 + NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 1,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%u", GetRabPercent(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_RAB_PERCENT,
        CX_SCREEN * 2 / 3 + NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 1,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    CreateWindow (MH_LABEL_NAME,
        MW_ROLL_OPEN_COM_CAPTION,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN * 2 / 3 + NEEDLE_ZERO_CAPTION_LEFT,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 2,
        NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%d", GetRollOpenCom(g_knit_data));
    CreateWindow(MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT_ROLL_OPEN,
        CX_SCREEN * 2 / 3 + NEEDLE_ZERO_CAPTION_LEFT + NEEDLE_ZERO_CAPTION_WIDTH,
        NEEDLE_ZERO_CAPTION_TOP + (NEEDLE_ZERO_CAPTION_HEIGHT + NEEDLE_ZERO_INTER) * 2,
        NEEDLE_ZERO_NAME_WIDTH,
        NEEDLE_ZERO_CAPTION_HEIGHT,
        hWnd, 0);

    const char *scc[7] = {
            GetString(588),
            GetString(607),
            GetString(608),
            GetString(609),
            GetString(616),
            GetString(619),
            GetString(625),
    };
    char edit[7][64] = {{0}};
    sprintf(edit[0], "%u", GetSelectStartSect(g_knit_data));
    strcat(edit[1], GetCamLockCaption(GetCamLock(g_knit_data)));
    strcat(edit[2], GetTakeYarnMode(g_knit_data) ? GetString(611) : GetString(610));
    strcat(edit[3], GetAutoHdDropEnable(g_knit_data) ? GetString(338) : GetString(339));
    strcat(edit[4], GetRockZeroDrt(g_knit_data) ? GetString(618) : GetString(617));
    sprintf(edit[5], "%d", GetDumuMaxValue(g_knit_data));
    sprintf(edit[6], "%d", GetProbeDistance(g_knit_data));

    int id[7] = {
            IDC_SELECT_START_SECT,
            IDC_EDIT_LOCK_CAM,
            IDC_TAKE_YARN_MODE,
            IDC_EDIT_HD,
            IDC_EDIT_ROCK,
            IDC_DUMU,
            IDC_EDIT_PROBE,
    };
    int i = 0;
    for(i = 0; i < 7; ++i)
    {
        CreateWindow(MH_LABEL_NAME,
            scc[i],
            WS_CHILD | WS_VISIBLE | BS_LEFT,
            IDC_STATIC,
            559,
            76 + 30 * (i + 3),
            160,
            24,
            hWnd, MH_LABEL_TEXT_GREEN | MH_LABEL_BKGND_BLACK);

        CreateWindow(MH_EDIT_NAME,
            edit[i],
            WS_CHILD | WS_VISIBLE,
            id[i],
            719,
            76 + 30 * (i + 3),
            60,
            24,
            hWnd, 0);
    }


    //needle
    sprintf(sc, "%s%s\n", MW_MAC_POS_CAPTION, ":");
    CreateWindow(MH_LABEL_NAME,
        sc,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_STATIC,
        CX_SCREEN / 2 - 80,
        CY_XTCS_WND_BMP - 77,
        80,
        20,
        hWnd, MH_LABEL_COLOR_1GROUP);
    short needle = 0;
    GetCurrentNeedle(g_knit_data, &needle);
    sprintf(sc, "%d", needle);
    CreateWindow(MH_LABEL_NAME,
        sc,
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_MAC_POS,
        CX_SCREEN / 2,
        CY_XTCS_WND_BMP - 77,
        60,
        20,
        hWnd, MH_LABEL_COLOR_3GROUP);
    //tip
    CreateWindow(MH_LABEL_NAME,
        GetString(385),
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_TIP,
        150,
        CY_XTCS_WND_BMP - 35,
        500,
        20,
        hWnd, MH_LABEL_BKGND_2GREEN | MH_LABEL_TEXT_YELLOW);
}
/*
 * set edit value to knit
 */
static void OnSetKnitValue(HWND hWnd, int nID)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
    switch(nID)
    {
    case IDC_EDIT_INCH_TYPE:
        if(data.pwnd_sys)
        {
            float f = atof(sc);
            SetInchType(g_knit_data, f);
            data.pwnd_sys->change.sys = TRUE;
            data.pwnd_sys->change.reset = TRUE;
        }
        break;
    case IDC_EDIT_SELECT_RGHT_COM:
        if(data.pwnd_sys)
        {
            int n = atoi(sc);
            SetSelectRightCom(g_knit_data, n);
            data.pwnd_sys->change.sys = TRUE;
        }
        break;
    case IDC_EDIT_SELECT_LEFT_COM:
        if(data.pwnd_sys)
        {
            int n = atoi(sc);
            SetSelectLeftCom(g_knit_data, n);
            data.pwnd_sys->change.sys = TRUE;
        }
        break;
    case IDC_EDIT_SELECT_SECT:
        if(data.pwnd_sys)
        {
            uchar n = atoi(sc);
            SetSelectSect(g_knit_data, n);
            data.pwnd_sys->change.mac = TRUE;
            data.pwnd_sys->change.reset = TRUE;
        }
        break;
    case IDC_EDIT_HIGH_CAM:
        if(data.pwnd_sys)
        {
            uchar n = atoi(sc);
            SetCamHighVoltage(g_knit_data, n);
            data.pwnd_sys->change.sys = TRUE;
        }
        break;
    case IDC_EDIT_HIGH_XZQ:
        if(data.pwnd_sys)
        {
            uchar n = atoi(sc);
            SetSelectHighVoltage(g_knit_data, n);
            data.pwnd_sys->change.sys = TRUE;
        }
        break;
    case IDC_EDIT_LOW_XZQ:
        if(data.pwnd_sys)
        {
            uchar n = atoi(sc);
            SetGauzeHighVoltage(g_knit_data, n);
            data.pwnd_sys->change.sys = TRUE;
        }
        break;
    case IDC_EDIT_DUMU_RESET:
        if(data.pwnd_sys)
        {
            uchar n = atoi(sc);
            SetDumuResetSpeed(g_knit_data, n);
            data.pwnd_sys->change.sys = TRUE;
        }
        break;
    case IDC_EDIT_DUMU_WORK:
        if(data.pwnd_sys)
        {
            uchar n = atoi(sc);
            SetDumuWorkSpeed(g_knit_data, n);
            data.pwnd_sys->change.sys = TRUE;
        }
        break;
    case IDC_EDIT_BELT_INTER:
        break;
    case IDC_EDIT_TOTAL_NEEDLE:
        if(data.pwnd_sys)
        {
            ushort n = atoi(sc);
            SetTotalNeedle(g_knit_data, n);
            data.pwnd_sys->change.sys = TRUE;
            data.pwnd_sys->change.reset = TRUE;
        }
        break;
    case IDC_EDIT_ROCK_SPEED:
        if(data.pwnd_sys)
        {
            uchar n = atoi(sc);
            SetRockSpeed(g_knit_data, n);
            data.pwnd_sys->change.sys = TRUE;
        }
        break;
    case IDC_EDIT_SINK_ENABLE:
        break;
    case IDC_EDIT_LANGUAGE:
        if(data.pwnd_sys)
        {
            uchar n = atoi(sc);
            SetLanguage(g_knit_data, n);
            data.pwnd_sys->change.mac = TRUE;
        }
        break;
    case IDC_EDIT_XZQ_REPEAT:
          if(data.pwnd_sys)
        {
            ushort n = atoi(sc);
            SetSelectRepeatEnable(g_knit_data, n);
            data.pwnd_sys->change.sys = TRUE;
            data.pwnd_sys->change.reset = TRUE;
        }
        break;
    case IDC_EDIT_HEAD_MOVE_ENABLE:
        break;
    case IDC_EDIT_ROCK_COM:
        if(data.pwnd_sys)
        {
            int n = atoi(sc);
            SetRockGapCom(g_knit_data, n);
            data.pwnd_sys->change.sys = TRUE;
        }
        break;
    case IDC_EDIT_ROCK_TYPE:
        if(data.pwnd_sys)
        {
            int n = atoi(sc);
            SetRockType(g_knit_data, n);
            data.pwnd_sys->change.sys = TRUE;
        }
        break;
    case IDC_EDIT_RAB_PERCENT:
        if(data.pwnd_sys)
        {
            uchar n = atoi(sc);
            SetRabPercent(g_knit_data, n);
            data.pwnd_sys->change.work = TRUE;
        }
        break;
    case IDC_EDIT_ROLL_OPEN:
        if(data.pwnd_sys)
        {
            int n = atoi(sc);
            SetRollOpenCom(g_knit_data, n);
            data.pwnd_sys->change.sys = TRUE;
        }
        break;
    case IDC_EDIT_PROBE:
        if(data.pwnd_sys)
        {
            short n = atoi(sc);
            SetProbeDistance(g_knit_data, n);
            data.pwnd_sys->change.mac = TRUE;
        }
        break;
    }
}
/*
 *
 */
static void OnChangeLanguage(HWND hWnd, ushort nID)
{
    uchar n = GetLanguage(g_knit_data);
    n = !n;
    SetLanguage(g_knit_data, n);
    const char *sc = n ? MW_ENGLISH_CAPTION : MW_CHINESE_CAPTION;

    HWND hTmp = GetDlgItem(hWnd, nID);
    SetWindowCaption(hTmp, sc);
    InvalidateRect(hTmp, NULL, TRUE);

    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    if(data.pwnd_sys)
    {
        data.pwnd_sys->change.other = TRUE;
    }
}
/*
 *
 */
static void OnChangeSelectorRepeat(HWND hWnd, ushort nID)
{
    uchar n = GetSelectRepeatEnable(g_knit_data);
    n = !n;
    SetSelectRepeatEnable(g_knit_data, n);
    const char *sc = n ? MW_OPEN_CAPTION : MW_CLOSE_CAPTION;
    SetWindowCaption(GetDlgItem(hWnd, nID), sc);
    InvalidateRect(GetDlgItem(hWnd, nID), NULL, TRUE);
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    if(data.pwnd_sys)
    {
        data.pwnd_sys->change.sys = TRUE;
    }
}
/*
 *
 */
static void OnChangeRockType(HWND hWnd, ushort nID)
{
    uchar n = GetRockType(g_knit_data);
    ++n;
    if(n > 2)
    {
        n = 0;
    }
    SetRockType(g_knit_data, n);
    const char *sc = n ? (n == 2 ? GetString(379) : GetString(378)) : GetString(377);
    SetWindowCaption(GetDlgItem(hWnd, nID), sc);
    InvalidateRect(GetDlgItem(hWnd, nID), NULL, TRUE);
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    if(data.pwnd_sys)
    {
        data.pwnd_sys->change.sys = TRUE;
    }
}
/*
 *
 */
static void OnChangeLockCam(HWND hWnd, ushort nID)
{
    uchar n = GetCamLock(g_knit_data);
    ++n;
    if(n > 2)
    {
        n = 0;
    }
    SetCamLock(g_knit_data, n);
    const char *sc;
    if(n == 2)
    {
        sc = GetString(613);
    }
    else if(n == 1)
    {
        sc = GetString(612);
    }
    else
    {
        sc = GetString(672);
    }
    HWND hTmp = GetDlgItem(hWnd, nID);
    SetWindowCaption(hTmp, sc);
    InvalidateRect(hTmp, NULL, TRUE);
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    if(data.pwnd_sys)
    {
        data.pwnd_sys->change.work = TRUE;
    }
}
/*
 *界面下方的提示
 */
static void OnChangeCtrl(HWND hWnd)
{
    HWND hTmp = GetFocusChild(hWnd);
    ushort nID = GetDlgCtrlID(hTmp);
    const char *sc = "";
    switch (nID)
    {
    case IDC_EDIT_NEEDLE_ZERO:
        sc = GetString(385);
        break;
    case IDC_EDIT_LEFT_YARN_RGHT_ZERO:
        sc = GetString(675);
        break;
    case IDC_EDIT_LEFT_YARN_LEFT_ZERO:
        sc = GetString(676);
        break;
    case IDC_EDIT_HEAD_LEFT_FORBID:
        sc = GetString(677);
        break;
    case IDC_EDIT_HEAD_RGHT_FORBID:
        sc = GetString(678);
        break;
    case IDC_EDIT_INCH_TYPE:
        sc = GetString(679);
        break;
    case IDC_EDIT_SELECT_RGHT_COM:
    case IDC_EDIT_SELECT_LEFT_COM:
        sc = GetString(680);
        break;
    case IDC_EDIT_SELECT_SECT:
        sc = GetString(681);
        break;
    case IDC_EDIT_HIGH_CAM:
        sc = GetString(682);
        break;
    case IDC_EDIT_HIGH_XZQ:
        sc = GetString(683);
        break;
    case IDC_EDIT_LOW_XZQ:
        sc = GetString(741);
        break;
    case IDC_EDIT_DUMU_RESET:
        sc = GetString(684);
        break;
    case IDC_EDIT_DUMU_WORK:
        sc = GetString(685);
        break;
    case IDC_EDIT_BELT_INTER:
        sc = GetString(686);
        break;
    case IDC_EDIT_TOTAL_NEEDLE:
        sc = GetString(687);
        break;
    case IDC_EDIT_ROCK_SPEED:
        sc = GetString(688);
        break;
    case IDC_EDIT_SINK_ENABLE:
        sc = GetString(689);
        break;
    case IDC_EDIT_LANGUAGE:
        sc = GetString(690);
        break;
    case IDC_EDIT_XZQ_REPEAT:
        sc = GetString(691);
        break;
    case IDC_EDIT_HEAD_MOVE_ENABLE:
        sc = GetString(692);
        break;
    case IDC_EDIT_ROCK_COM:
        sc = GetString(693);
        break;
    case IDC_EDIT_ROCK_TYPE:
        sc = GetString(694);
        break;
    case IDC_EDIT_RAB_PERCENT:
        sc = GetString(695);
        break;
    case IDC_EDIT_ROLL_OPEN:
        break;
    case IDC_SELECT_START_SECT:
        break;
    case IDC_EDIT_LOCK_CAM:
        break;
    case IDC_DUMU:
        break;
    case IDC_EDIT_PROBE:
        break;
    }
    hTmp = GetDlgItem(hWnd, IDC_TIP);
    SetWindowCaption(hTmp, sc);
    InvalidateRect(hTmp, NULL, TRUE);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    switch(nID)
    {
    case ID_ESC:
        {
            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
            if(data.pwnd_sys)
            {
                if(data.pwnd_sys->sys_query_state)
                {
                    data.pwnd_sys->sys_query_state = FALSE;
                    pthread_join(data.pwnd_sys->sys_thread_id, NULL);
                }
                if(data.pwnd_sys->change.sys || data.pwnd_sys->change.other ||
                        data.pwnd_sys->change.work || data.pwnd_sys->change.mac)
                {
                    int ret = WriteMacFile(MAC_PARAM_FILE, g_knit_data);
                    if(ret)
                    {
                        printf("write param: %d\n", ret);
                        AfxInfoBox(hWnd, GetString(392), GetString(481));
                    }
                    else
                    {
                        if(data.pwnd_sys->change.sys)
                        {
                            SendSysParam(g_knit_data);
                        }
                        if(data.pwnd_sys->change.work)
                        {
                            SendWorkParam(g_knit_data);
                        }
                        if(data.pwnd_sys->change.mac)
                        {
                            SendPartsParam(g_knit_data);
                        }
                        if(data.pwnd_sys->change.reset)
                        {
                            SetWhichNeedDo(g_knit_data, NEED_DO_RESET_START);
                        }
                        MH_FILE_SYNC();
                    }
                }
            }
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
	    }
		break;
	case IDC_EDIT_BELT_INTER:
	    {
	        CreateBeltWindow(hWnd);
	    }
	    break;
	case IDC_EDIT_LANGUAGE:
	    {
	    }
	    break;
	case IDC_EDIT_NEEDLE_ZERO:
	case IDC_EDIT_LEFT_YARN_RGHT_ZERO:
	case IDC_EDIT_LEFT_YARN_LEFT_ZERO:
	case IDC_EDIT_RGHT_YARN_RGHT_ZERO:
	case IDC_EDIT_RGHT_YARN_LEFT_ZERO:
	    {

            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
	        int ret = 0;
	        int needle = 0;
	        char sc[32] = {0};
	        ret = GetCurrentPulse(g_knit_data, &needle);
	        if(ret)
            {
                printf("get current pulse error\n");
            }
             if(AfxMessageBox(hWnd, NULL, GetString(585)))
             {
                  switch(nID)
                {
                  case IDC_EDIT_NEEDLE_ZERO:
                      {
                          SetNeedleZero(g_knit_data, needle);
                      }
                      break;
                    case IDC_EDIT_LEFT_YARN_RGHT_ZERO:
                        {
                            needle = GetGouZhenCom(hWnd, needle);
                            SetLeftYarnRightCom(g_knit_data, needle);
                        }
                        break;
                    case IDC_EDIT_LEFT_YARN_LEFT_ZERO:
                        {
                            needle = GetGouZhenCom(hWnd, needle);
                            SetLeftYarnLeftCom(g_knit_data, needle);
                        }
                        break;
                    case IDC_EDIT_RGHT_YARN_LEFT_ZERO:
                        {
                            needle = GetGouZhenCom(hWnd, needle);
                            SetRightYarnLeftCom(g_knit_data, needle);
                        }
                        break;
                    case IDC_EDIT_RGHT_YARN_RGHT_ZERO:
                        {
                            needle = GetGouZhenCom(hWnd, needle);
                            SetRightYarnRightCom(g_knit_data, needle);
                        }
                        break;
                }
                HWND hWndTmp = GetDlgItem(hWnd, nID);
                sprintf(sc, "%d", needle);
                SetWindowCaption(hWndTmp, sc);
                InvalidateRect(hWndTmp, NULL, TRUE);
                data.pwnd_sys->change.sys = TRUE;
             }
	    }
	    break;
	case IDC_EDIT_INCH_TYPE:
	case IDC_EDIT_SELECT_RGHT_COM:
	case IDC_EDIT_SELECT_LEFT_COM:
	case IDC_EDIT_SELECT_SECT:
	case IDC_EDIT_HIGH_CAM:
	case IDC_EDIT_HIGH_XZQ:
	case IDC_EDIT_LOW_XZQ:
	case IDC_EDIT_DUMU_RESET:
	case IDC_EDIT_DUMU_WORK:
	case IDC_EDIT_TOTAL_NEEDLE:
	case IDC_EDIT_ROCK_SPEED:
	case IDC_EDIT_ROCK_COM:
	case IDC_EDIT_RAB_PERCENT:
	case IDC_EDIT_ROLL_OPEN:
	case IDC_EDIT_PROBE:
	    {
	        int min = -100000;
	        int max = 100000;
	        switch(nID)
	        {
	        case IDC_EDIT_INCH_TYPE:
	            min = 0;
	            max = 16;
	            break;
	        case IDC_EDIT_SELECT_RGHT_COM:
	            min = -79;
	            max = 79;
	            break;
	        case IDC_EDIT_SELECT_LEFT_COM:
	            min = -79;
	            max = 79;
	            break;
	        case IDC_EDIT_SELECT_SECT:
	            min = 1;
	            max = 8;
	            break;
	        case IDC_EDIT_HIGH_CAM:
	            min = 1;
	            max = 60;
	            break;
	        case IDC_EDIT_HIGH_XZQ:
	            min = 1;
	            max = 10;
	            break;
	        case IDC_EDIT_LOW_XZQ:
	            min = 1;
	            max = 60;
	            break;
	        case IDC_EDIT_DUMU_RESET:
	            min = 1;
	            max = 80;
	            break;
	        case IDC_EDIT_DUMU_WORK:
	            min = 1;
	            max = 30;
	            break;
	        case IDC_EDIT_TOTAL_NEEDLE:
	            min = 1;
	            max = 2000;
	            break;
	        case IDC_EDIT_ROCK_SPEED:
	            min = 1;
	            max = 10;
	            break;
	        case IDC_EDIT_ROCK_COM:
	            min = 1;
	            max = 100;
	            break;
	        case IDC_EDIT_RAB_PERCENT:
	            min = 0;
	            max = 100;
	            break;
	        case IDC_EDIT_ROLL_OPEN:
	            min = 0;
	            max = 100;
	            break;
	        case IDC_EDIT_PROBE:
	            min = -100;
	            max = 100;
	            break;
	        }
	        HWND hTmp = GetDlgItem(hWnd, nID);
            RECT rect;
            GetClientRect(hTmp, &rect);
            ClientToScreen(hTmp, &rect.left, &rect.top);
            ClientToScreen(hTmp, &rect.right, &rect.bottom);
            int value = atoi(GetWindowCaption(hTmp));
            CreateKeyboardWindow(hWnd, rect, min, max, value);
	    }
	    break;
	case IDC_EDIT_SINK_ENABLE:
	    {
	         CreateSinkDrtWindow(hWnd);
	    }
	    break;
	case IDC_EDIT_XZQ_REPEAT:
	    {
	        OnChangeSelectorRepeat(hWnd, nID);
	    }
	    break;
	case IDC_EDIT_HEAD_MOVE_ENABLE:
	    {
	    }
	    break;
	case IDC_EDIT_ROCK_TYPE:
	    {
	        OnChangeRockType(hWnd, nID);
	    }
	    break;
	case IDC_SELECT_START_SECT:
	    {
	        CreateSelectWindow(hWnd);
	    }
	    break;
	case IDC_EDIT_LOCK_CAM:
	    {
	        OnChangeLockCam(hWnd, nID);
	    }
	    break;
	case IDC_DUMU:
	    {
	        CreateDumuDistWindow(hWnd);
	    }
	    break;
	case ID_COPY:
	    {
	        char sc[MH_FILE_NAME_MAX_LEN] = {0};
	        strcat(sc, BACKUP_MAC_PARAM_FILE);
	        int ret = WriteMacFile(sc, g_knit_data);
	        sc[0] = 0;
	        if(ret)
	        {
	            printf("Failed to backup mac file: %d\n", ret);
	            strcat(sc, GetString(475));
	        }
	        else
	        {
	            MH_FILE_SYNC();
	            strcat(sc, GetString(474));
	        }
	        AfxInfoBox(hWnd, NULL, sc);
	    }
	    break;
	}
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
	if(key == SCANCODE_ESCAPE)
	{
	    OnBtnCmd(hWnd, ID_ESC);
	}
	else if(key == SCANCODE_CURSORBLOCKLEFT)
	{
	    uint nID = 0;
	    switch(GetDlgCtrlID(GetFocusChild(hWnd)))
	    {
	    case IDC_EDIT_LOW_XZQ:
	        nID = IDC_EDIT_NEEDLE_ZERO;
	        break;
	    case IDC_EDIT_DUMU_RESET:
	        nID = IDC_EDIT_LEFT_YARN_RGHT_ZERO;
	        break;
	    case IDC_EDIT_DUMU_WORK:
	        nID = IDC_EDIT_LEFT_YARN_LEFT_ZERO;
	        break;
	    case IDC_EDIT_BELT_INTER:
	        nID = IDC_EDIT_RGHT_YARN_RGHT_ZERO;
	        break;
	    case IDC_EDIT_TOTAL_NEEDLE:
	        nID = IDC_EDIT_RGHT_YARN_LEFT_ZERO;
	        break;
	    case IDC_EDIT_ROCK_SPEED:
	        nID = IDC_EDIT_INCH_TYPE;
	        break;
	    case IDC_EDIT_SINK_ENABLE:
	        nID = IDC_EDIT_SELECT_RGHT_COM;
	        break;
	    case IDC_EDIT_LANGUAGE:
	        nID = IDC_EDIT_SELECT_LEFT_COM;
	        break;
	    case IDC_EDIT_XZQ_REPEAT:
	        nID = IDC_EDIT_SELECT_SECT;
	        break;
	    case IDC_EDIT_HEAD_MOVE_ENABLE:
	        nID = IDC_EDIT_HIGH_CAM;
	        break;
	    case IDC_EDIT_ROCK_COM:
	        nID = IDC_EDIT_HIGH_XZQ;
	        break;
	    case IDC_EDIT_ROCK_TYPE:
	        nID = IDC_EDIT_LOW_XZQ;
	        break;
	    case IDC_EDIT_RAB_PERCENT:
	        nID = IDC_EDIT_DUMU_RESET;
	        break;
	    case IDC_EDIT_ROLL_OPEN:
	        nID = IDC_EDIT_DUMU_WORK;
	        break;
	    case IDC_SELECT_START_SECT:
	        nID = IDC_EDIT_BELT_INTER;
	        break;
	    case IDC_EDIT_LOCK_CAM:
	        nID = IDC_EDIT_TOTAL_NEEDLE;
	        break;
	    case IDC_TAKE_YARN_MODE:
	        nID = IDC_EDIT_ROCK_SPEED;
	        break;
	    case IDC_EDIT_HD:
	        nID = IDC_EDIT_SINK_ENABLE;
	        break;
	    case IDC_EDIT_ROCK:
	        nID = IDC_EDIT_LANGUAGE;
	        break;
	    case IDC_DUMU:
	        nID = IDC_EDIT_XZQ_REPEAT;
	        break;
	    case IDC_EDIT_PROBE:
	        nID = IDC_EDIT_HEAD_MOVE_ENABLE;
	        break;
	    default:
	        break;
	    }
	    if(nID)
	    {
	        SetFocusChild(GetDlgItem(hWnd, nID));
	    }
	}
	else if(key == SCANCODE_CURSORBLOCKRIGHT)
	{
	    uint nID = 0;
	    switch(GetDlgCtrlID(GetFocusChild(hWnd)))
	    {
	    case IDC_EDIT_NEEDLE_ZERO:
	        nID = IDC_EDIT_LOW_XZQ;
	        break;
	    case IDC_EDIT_LEFT_YARN_RGHT_ZERO:
	        nID = IDC_EDIT_DUMU_RESET;
	        break;
	    case IDC_EDIT_LEFT_YARN_LEFT_ZERO:
	        nID = IDC_EDIT_DUMU_WORK;
	        break;
	    case IDC_EDIT_RGHT_YARN_RGHT_ZERO:
	        nID = IDC_EDIT_BELT_INTER;
	        break;
	    case IDC_EDIT_RGHT_YARN_LEFT_ZERO:
	        nID = IDC_EDIT_TOTAL_NEEDLE;
	        break;
	    case IDC_EDIT_INCH_TYPE:
	        nID = IDC_EDIT_ROCK_SPEED;
	        break;
	    case IDC_EDIT_SELECT_RGHT_COM:
	        nID = IDC_EDIT_SINK_ENABLE;
	        break;
	    case IDC_EDIT_SELECT_LEFT_COM:
	        nID = IDC_EDIT_LANGUAGE;
	        break;
	    case IDC_EDIT_SELECT_SECT:
	        nID = IDC_EDIT_XZQ_REPEAT;
	        break;
	    case IDC_EDIT_HIGH_CAM:
	        nID = IDC_EDIT_HEAD_MOVE_ENABLE;
	        break;
	    case IDC_EDIT_HIGH_XZQ:
	        nID = IDC_EDIT_ROCK_COM;
	        break;
	    case IDC_EDIT_LOW_XZQ:
	        nID = IDC_EDIT_ROCK_TYPE;
	        break;
	    case IDC_EDIT_DUMU_RESET:
	        nID = IDC_EDIT_RAB_PERCENT;
	        break;
	    case IDC_EDIT_DUMU_WORK:
	        nID = IDC_EDIT_ROLL_OPEN;
	        break;
	    case IDC_EDIT_BELT_INTER:
            nID = IDC_SELECT_START_SECT;
            break;
        case IDC_EDIT_TOTAL_NEEDLE:
            nID = IDC_EDIT_LOCK_CAM;
            break;
        case IDC_EDIT_ROCK_SPEED:
            nID = IDC_TAKE_YARN_MODE;
            break;
        case IDC_EDIT_SINK_ENABLE:
            nID = IDC_EDIT_HD;
            break;
        case IDC_EDIT_LANGUAGE:
            nID = IDC_EDIT_ROCK;
            break;
        case IDC_EDIT_XZQ_REPEAT:
            nID = IDC_DUMU;
            break;
        case IDC_EDIT_HEAD_MOVE_ENABLE:
            nID = IDC_EDIT_PROBE;
            break;
	    default:
	        break;
	    }
        if(nID)
	    {
	        SetFocusChild(GetDlgItem(hWnd, nID));
	    }
	}
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        uint nID = 0;
        switch(GetDlgCtrlID(GetFocusChild(hWnd)))
        {
	    case IDC_EDIT_LEFT_YARN_RGHT_ZERO:
	        nID = IDC_EDIT_NEEDLE_ZERO;
	        break;
	    case IDC_EDIT_LEFT_YARN_LEFT_ZERO:
	        nID = IDC_EDIT_LEFT_YARN_RGHT_ZERO;
	        break;
	    case IDC_EDIT_RGHT_YARN_RGHT_ZERO:
	        nID = IDC_EDIT_LEFT_YARN_LEFT_ZERO;
	        break;
	    case IDC_EDIT_RGHT_YARN_LEFT_ZERO:
	        nID = IDC_EDIT_RGHT_YARN_RGHT_ZERO;
	        break;
	    case IDC_EDIT_INCH_TYPE:
	        nID = IDC_EDIT_RGHT_YARN_LEFT_ZERO;
	        break;
	    case IDC_EDIT_SELECT_RGHT_COM:
	        nID = IDC_EDIT_INCH_TYPE;
	        break;
	    case IDC_EDIT_SELECT_LEFT_COM:
	        nID = IDC_EDIT_SELECT_RGHT_COM;
	        break;
	    case IDC_EDIT_SELECT_SECT:
	        nID = IDC_EDIT_SELECT_LEFT_COM;
	        break;
	    case IDC_EDIT_HIGH_CAM:
	        nID = IDC_EDIT_SELECT_SECT;
	        break;
	    case IDC_EDIT_HIGH_XZQ:
	        nID = IDC_EDIT_HIGH_CAM;
	        break;
	    case IDC_EDIT_DUMU_RESET:
	        nID = IDC_EDIT_LOW_XZQ;
	        break;
	    case IDC_EDIT_DUMU_WORK:
	        nID = IDC_EDIT_DUMU_RESET;
	        break;
	    case IDC_EDIT_BELT_INTER:
	        nID = IDC_EDIT_DUMU_WORK;
	        break;
	    case IDC_EDIT_TOTAL_NEEDLE:
	        nID = IDC_EDIT_BELT_INTER;
	        break;
	    case IDC_EDIT_ROCK_SPEED:
	        nID = IDC_EDIT_TOTAL_NEEDLE;
	        break;
	    case IDC_EDIT_SINK_ENABLE:
	        nID = IDC_EDIT_ROCK_SPEED;
	        break;
	    case IDC_EDIT_LANGUAGE:
	        nID = IDC_EDIT_SINK_ENABLE;
	        break;
	    case IDC_EDIT_XZQ_REPEAT:
	        nID = IDC_EDIT_LANGUAGE;
	        break;
	    case IDC_EDIT_HEAD_MOVE_ENABLE:
	        nID = IDC_EDIT_XZQ_REPEAT;
	        break;
	    case IDC_EDIT_ROCK_COM:
	        nID = IDC_EDIT_HEAD_MOVE_ENABLE;
	        break;
	    case IDC_EDIT_RAB_PERCENT:
	        nID = IDC_EDIT_ROCK_TYPE;
	        break;
	    case IDC_EDIT_ROLL_OPEN:
	        nID = IDC_EDIT_RAB_PERCENT;
	        break;
	    case IDC_SELECT_START_SECT:
            nID = IDC_EDIT_ROLL_OPEN;
            break;
        case IDC_EDIT_LOCK_CAM:
            nID = IDC_SELECT_START_SECT;
            break;
        case IDC_TAKE_YARN_MODE:
            nID = IDC_EDIT_LOCK_CAM;
            break;
        case IDC_EDIT_HD:
            nID = IDC_TAKE_YARN_MODE;
            break;
        case IDC_EDIT_ROCK:
            nID = IDC_EDIT_HD;
            break;
        case IDC_DUMU:
            nID = IDC_EDIT_ROCK;
            break;
        case IDC_EDIT_PROBE:
            nID = IDC_DUMU;
            break;
        default:
            break;
        }
        if(nID)
	    {
	        SetFocusChild(GetDlgItem(hWnd, nID));
	    }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        uint nID = 0;
        switch(GetDlgCtrlID(GetFocusChild(hWnd)))
        {
        case IDC_EDIT_NEEDLE_ZERO:
            nID = IDC_EDIT_LEFT_YARN_RGHT_ZERO;
            break;
	    case IDC_EDIT_LEFT_YARN_RGHT_ZERO:
	        nID = IDC_EDIT_LEFT_YARN_LEFT_ZERO;
	        break;
	    case IDC_EDIT_LEFT_YARN_LEFT_ZERO:
	        nID = IDC_EDIT_RGHT_YARN_RGHT_ZERO;
	        break;
	    case IDC_EDIT_RGHT_YARN_RGHT_ZERO:
	        nID = IDC_EDIT_RGHT_YARN_LEFT_ZERO;
	        break;
	    case IDC_EDIT_RGHT_YARN_LEFT_ZERO:
	        nID = IDC_EDIT_INCH_TYPE;
	        break;
	    case IDC_EDIT_INCH_TYPE:
	        nID = IDC_EDIT_SELECT_RGHT_COM;
	        break;
	    case IDC_EDIT_SELECT_RGHT_COM:
	        nID = IDC_EDIT_SELECT_LEFT_COM;
	        break;
	    case IDC_EDIT_SELECT_LEFT_COM:
	        nID = IDC_EDIT_SELECT_SECT;
	        break;
	    case IDC_EDIT_SELECT_SECT:
	        nID = IDC_EDIT_HIGH_CAM;
	        break;
	    case IDC_EDIT_HIGH_CAM:
	        nID = IDC_EDIT_HIGH_XZQ;
	        break;
	    case IDC_EDIT_LOW_XZQ:
	        nID = IDC_EDIT_DUMU_RESET;
	        break;
	    case IDC_EDIT_DUMU_RESET:
	        nID = IDC_EDIT_DUMU_WORK;
	        break;
	    case IDC_EDIT_DUMU_WORK:
	        nID = IDC_EDIT_BELT_INTER;
	        break;
	    case IDC_EDIT_BELT_INTER:
	        nID = IDC_EDIT_TOTAL_NEEDLE;
	        break;
	    case IDC_EDIT_TOTAL_NEEDLE:
	        nID = IDC_EDIT_ROCK_SPEED;
	        break;
	    case IDC_EDIT_ROCK_SPEED:
	        nID = IDC_EDIT_SINK_ENABLE;
	        break;
	    case IDC_EDIT_SINK_ENABLE:
	        nID = IDC_EDIT_LANGUAGE;
	        break;
	    case IDC_EDIT_LANGUAGE:
	        nID = IDC_EDIT_XZQ_REPEAT;
	        break;
	    case IDC_EDIT_XZQ_REPEAT:
	        nID = IDC_EDIT_HEAD_MOVE_ENABLE;
	        break;
	    case IDC_EDIT_HEAD_MOVE_ENABLE:
	        nID = IDC_EDIT_ROCK_COM;
	        break;
	    case IDC_EDIT_ROCK_TYPE:
	        nID = IDC_EDIT_RAB_PERCENT;
	        break;
	    case IDC_EDIT_RAB_PERCENT:
	        nID = IDC_EDIT_ROLL_OPEN;
	        break;
	    case IDC_EDIT_ROLL_OPEN:
            nID = IDC_SELECT_START_SECT;
            break;
        case IDC_SELECT_START_SECT:
            nID = IDC_EDIT_LOCK_CAM;
            break;
        case IDC_EDIT_LOCK_CAM:
            nID = IDC_TAKE_YARN_MODE;
            break;
        case IDC_TAKE_YARN_MODE:
            nID = IDC_EDIT_HD;
            break;
        case IDC_EDIT_HD:
            nID = IDC_EDIT_ROCK;
            break;
        case IDC_EDIT_ROCK:
            nID = IDC_DUMU;
            break;
        case IDC_DUMU:
            nID = IDC_EDIT_PROBE;
            break;
        default:
            break;
        }
        if(nID)
	    {
	        SetFocusChild(GetDlgItem(hWnd, nID));
	    }
    }
    else if(key == SCANCODE_ENTER)
    {
        WND_EXTRA_DATA data;
        data.value = GetWindowAdditionalData(hWnd);
        HWND hTmp = GetFocusChild(hWnd);
        ushort nID = GetDlgCtrlID(hTmp);
        if(nID == IDC_EDIT_LEFT_YARN_RGHT_ZERO || nID == IDC_EDIT_LEFT_YARN_LEFT_ZERO ||
           nID == IDC_EDIT_RGHT_YARN_LEFT_ZERO || nID == IDC_EDIT_RGHT_YARN_RGHT_ZERO)
        {
            OnBtnCmd(hWnd, nID);
        }
        else if(nID == IDC_TAKE_YARN_MODE)
        {
            int n = GetTakeYarnMode(g_knit_data);
            n = !n;
            SetTakeYarnMode(g_knit_data, n);
            HWND hTmp = GetDlgItem(hWnd, nID);
            SetWindowCaption(hTmp, n ? GetString(611) : GetString(610));
            InvalidateRect(hTmp, NULL, TRUE);

            if(data.pwnd_sys)
            {
                data.pwnd_sys->change.sys = TRUE;
            }
        }
        else if(nID == IDC_EDIT_HD)
        {
            int n = GetAutoHdDropEnable(g_knit_data);
            n = !n;
            SetAutoHdDropEnable(g_knit_data, n);
            HWND hTmp = GetDlgItem(hWnd, nID);
            SetWindowCaption(hTmp, n ? GetString(338) : GetString(339));
            InvalidateRect(hTmp, NULL, TRUE);

            if(data.pwnd_sys)
            {
                data.pwnd_sys->change.sys = TRUE;
            }
        }
        else if(nID == IDC_EDIT_ROCK)
        {
            int n = GetRockZeroDrt(g_knit_data);
            n = !n;
            SetRockZeroDrt(g_knit_data, n);

            HWND hTmp = GetDlgItem(hWnd, nID);
            SetWindowCaption(hTmp, n ? GetString(618) : GetString(617));
            InvalidateRect(hTmp, NULL, TRUE);

            if(data.pwnd_sys)
            {
                data.pwnd_sys->change.sys = TRUE;
            }
        }
        else if(nID == IDC_EDIT_HEAD_MOVE_ENABLE)
        {
            CreateRabModeWindow(hWnd);
        }
        else if(nID == IDC_EDIT_LANGUAGE)
        {
            OnChangeLanguage(hWnd, nID);
        }
        else if(nID == IDC_EDIT_XZQ_REPEAT)
        {
            OnChangeSelectorRepeat(hWnd, nID);
        }
        else if(nID == IDC_EDIT_ROCK_TYPE)
        {
            OnChangeRockType(hWnd, nID);
        }
        else if(nID == IDC_EDIT_LOCK_CAM)
        {
            OnChangeLockCam(hWnd, nID);
        }
        else
        {
            OnBtnCmd(hWnd, nID);
        }
    }
    else if(key == SCANCODE_F2)
    {
        OnBtnCmd(hWnd, ID_COPY);
    }
}
/*
 * update needle from machine
 */
static void *UpdateCurrentNeedle(void *arg)
{
    HWND hWnd = (HWND)arg;
    WND_SYS *p = (WND_SYS *)GetWindowAdditionalData(hWnd);
    short needle = 0;
    while(p->sys_query_state)
    {
        if(1)
        {
  //          int ret = GetCurrentNeedle(g_knit_data, &needle);
            int pulse = 0;
            int ret = GetCurrentPulse(g_knit_data, &pulse);
            if(!ret)
            {
                int rate = GetBeltPitchAdj(g_knit_data);
                if(rate)
                {
                //    needle = GetTotalNeedle(g_knit_data) * pulse / rate;
                    needle = (pulse - GetNeedleZero(g_knit_data)) * GetTotalNeedle(g_knit_data) /rate;
                }
                HWND hWndTmp = GetDlgItem(hWnd, IDC_MAC_POS);
                short n = atoi(GetWindowCaption(hWndTmp));
                if(n != needle)
                {
                    char sc[32] = {0};
                    sprintf(sc, "%d", needle);
                    SetWindowCaption(hWndTmp, sc);
                    InvalidateRect(hWndTmp, NULL, TRUE);
                }
            }
            usleep(20000);
        }
        else
        {
            usleep(50000);
        }
    }
    pthread_exit(0);
}
/*
 *
 */
static int SysWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
    {
    case MSG_CREATE:
        {
        	CreateSysInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            WND_SYS *p = (WND_SYS *)GetWindowAdditionalData(hWnd);
            if(p)
            {
                if(!p->sys_query_state)
                {
                    p->sys_query_state = TRUE;
                    if(pthread_create(&p->sys_thread_id, NULL, (void*)UpdateCurrentNeedle, (void*)hWnd))
                    {
                        printf("failed to create system param thread\n");
                        p->sys_query_state = FALSE;
                    }
                }
            }
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 200;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 200;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
            WND_SYS *p = (WND_SYS *)GetWindowAdditionalData(hWnd);
            if(p)
            {
                free(p);
                SetWindowAdditionalData(hWnd, 0);
            }
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
            OnChangeCtrl(hWnd);
        }
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_XTCS]);
        }
    	return 0;
    case MSG_COMMAND:
        {
            OnChangeCtrl(hWnd);
        	OnBtnCmd(hWnd, LOWORD(wParam));
        }
    	return 0;
    case MSG_SETDATA:
        {
            OnSetKnitValue(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_SETFOCUS:
        {
        }
        break;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateSysWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_XTCS_WND_BMP) / 2;
    int y = (CY_SCREEN - CY_XTCS_WND_BMP) / 2;
    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_SYS_TITLE_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = SysWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_XTCS_WND_BMP;
    CreateInfo.by = y + CY_XTCS_WND_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = (int)calloc(1, sizeof(WND_SYS));
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SwitchToKnitState(g_knit_data, FALSE);
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT_NEEDLE_ZERO));
    	ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
